home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / yk211src.lha / Yak_2.11_Src / Prefs / main.c < prev    next >
C/C++ Source or Header  |  1995-11-16  |  26KB  |  1,085 lines

  1. /*
  2.  * Yak version 2.11
  3.  * ----------------
  4.  * [Yak == Yet Another K(?)ommodity
  5.  *
  6.  * There seems to be a profusion of commodities doing this or that.
  7.  * Heres mine, to do what I want it to:
  8.  *
  9.  *      AutoActivate windows (SunMouse)
  10.  *      ClickToFront, ClickToBack, ScreenCycle
  11.  *      Close/Zip/Shrink/Zoom/Turn a window via keyboard.
  12.  *      Bring up a palette on front screen.
  13.  *      Insert date into read-stream.
  14.  *      Produce key-click (like my keyclick program).
  15.  *      Some other things...
  16.  *
  17.  * Martin W. Scott, Gaël Marziou & Philippe Bastiani, 7/94.
  18.  */
  19. #define __USE_SYSBASE 
  20.  
  21. #include <exec/types.h>
  22. #include <exec/libraries.h>
  23. #include <exec/memory.h>
  24. #include <devices/inputevent.h>
  25. #include <diskfont/diskfont.h>
  26. #include <dos/dos.h>
  27. #include <dos/dostags.h>
  28. #include <dos/notify.h>
  29. #include <graphics/displayinfo.h>
  30. #include <libraries/commodities.h>
  31. #include <libraries/reqtools.h>
  32. #include <libraries/locale.h>
  33. #include <intuition/intuitionbase.h>
  34. #include <workbench/workbench.h>
  35. #include <workbench/startup.h>
  36. #include <clib/alib_protos.h>
  37.  
  38. /* AMIGAGUIDE */
  39. #ifdef GADTOOLS
  40. #include <libraries/amigaguide.h>
  41. #include <proto/amigaguide.h>
  42. #endif
  43.  
  44. /* MUI */
  45. #ifdef MUI      /* NMC */
  46. #include <libraries/mui.h>
  47. #include <proto/muimaster.h>
  48. #endif          /* NMC */
  49.  
  50. /* BGUI */
  51. #ifdef BGUI     /* NMC */
  52. #include <libraries/bgui.h>
  53. #include <libraries/bgui_macros.h>
  54. #include <proto/bgui.h>
  55. #endif
  56.  
  57. #include <proto/exec.h>
  58. #include <proto/dos.h>
  59. #include <proto/graphics.h>
  60. #include <proto/commodities.h>
  61. #include <proto/diskfont.h>
  62. #include <proto/icon.h>
  63. #include <proto/iffparse.h>
  64. #include <proto/intuition.h>
  65. #include <proto/keymap.h>
  66. #include <proto/layers.h>
  67. #include <proto/locale.h>
  68. #include <proto/reqtools.h>
  69. #include <proto/wb.h>
  70.  
  71. #include <string.h>
  72. #include <stdarg.h>
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75.  
  76. #include "code.h"
  77. #include "yak.h"
  78. #include "Handlers.h"
  79. #include "hotkey_types.h"
  80. #include "icon.h"
  81. #include "gui.h"
  82. #include "Settings.h"
  83. #include "Requesters.h"
  84. #include "Version.h"
  85.  
  86. #include "yak_locale_strings.h"
  87.  
  88. /* local prototypes for main.c */
  89. static void CloseResources(void);
  90. static BOOL OpenResources(void);
  91. static void FreePatterns(void);
  92. static LONG ProcessMsg(void);
  93. static void NotifyNewPrefs(void);
  94. static SAVEDS LONG ASM HandleAppMsg(REG(a0) struct Hook *hook,REG(a2) APTR obj,REG(a1) struct AppMessage **amsg);
  95. void STACKARGS MAIN(char *);
  96.  
  97. LONG (*HandleIDCMP)(void);        /* GadTools */
  98. LONG (*HandleReturnID)(LONG ID);  /* MUI & BGUI */
  99.  
  100. extern struct WBStartup *WBMsg;
  101.  
  102. /*
  103.  *  libraries opened by startup code; basepointers needed by function pragmas
  104.  */
  105.  
  106. #ifdef _DCC
  107. extern struct ExecBase    *SysBase;
  108. extern struct DosLibrary  *DOSBase;
  109.  
  110. struct Library  *CxBase, *DiskfontBase,
  111.                 *IFFParseBase, *KeymapBase;
  112. #endif
  113.  
  114.  
  115.  
  116. BOOL CreateIcons;
  117.  
  118. char *ProgramName=NULL;
  119. char *PrefsFile=NULL;
  120.  
  121. IMPORT char FontName[];
  122. IMPORT int  FontSize;
  123. IMPORT struct TextFont *TextFont;
  124. IMPORT struct TextAttr Attr;    /*NMC*/
  125.  
  126. BYTE            broker_pri;
  127. struct MsgPort *broker_mp=NULL;
  128.  
  129. CxObj          *broker=NULL;
  130.  
  131. extern struct MsgPort *AppMsgPort;
  132. extern ULONG           appsigflag;
  133.  
  134. ULONG           wndsigflag;        /* here for overlay purposes */
  135. ULONG           cxsigflag=0L;
  136. ULONG           invariantsigflag;
  137.  
  138. BOOL NewPrefs=FALSE;
  139.  
  140. static LONG notifysigbit;
  141. static ULONG notifysigflag=0L;
  142. struct NotifyRequest NotifyRequest;
  143.  
  144. BOOL YakIconified=FALSE;
  145.  
  146. /* Amigaguide */
  147. char *PrefsHelp=NULL;
  148.  
  149.  
  150. #if defined(GADTOOLS) || defined(BGUI)
  151.  
  152. /*
  153.  * Determine GUI font according to FONT tooltype        NMC
  154.  * Used by both GADTOOLS and BGUI.
  155.  */
  156.  
  157. VOID
  158. ReadFont( char *Name, int *Size)
  159. {
  160.     if (sscanf(TTString("FONT", ""),"%s %d", Name, Size) >= 2)
  161.     {
  162.         if (Name[0] == '"')
  163.         {
  164.             sscanf(TTString("FONT", ""),"\" %s %d", Name, Size);
  165.         }
  166.         strcat(Name, ".font");
  167.     }
  168.     else
  169.     {
  170.         Name[0] = '\0';
  171.         *Size    = 8;
  172.     }
  173. }
  174. #endif
  175.  
  176.  
  177. #ifdef MUI
  178. /*
  179.  *
  180.  *                   MUI Specific Code
  181.  *
  182.  */
  183.  
  184. struct Library *MUIMasterBase;
  185.  
  186. APTR App=NULL;
  187.  
  188. struct Hook AppMsgHook = { { NULL,NULL },(void *)HandleAppMsg,NULL,NULL };
  189.  
  190. /* close what we opened */
  191. static void
  192. CloseResources()
  193. {
  194.         /* NULL pointers are valid so don't waste time to test them */
  195.         CloseLibrary(MUIMasterBase);
  196.         CloseLibrary(CxBase);
  197.         CloseLibrary(DiskfontBase);
  198.         CloseLibrary(IFFParseBase);
  199.         CloseLibrary(KeymapBase);
  200.         CloseLocaleStuff();
  201. }
  202.  
  203.  
  204. /* open libraries, devices that we need */
  205. static BOOL
  206. OpenResources(void)
  207. {
  208.         if ((MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN)) &&
  209.             (CxBase        = OpenLibrary("commodities.library", 37L)) &&
  210.             (DiskfontBase  = OpenLibrary("diskfont.library", 36L)) &&
  211.             (IFFParseBase  = OpenLibrary("iffparse.library", 37L)) &&
  212.             (KeymapBase    = OpenLibrary("keymap.library", 37L)))
  213.         {
  214.                 return TRUE;
  215.         }
  216.         CloseResources();
  217.         return FALSE;
  218. }
  219.  
  220.  
  221. BOOL 
  222. CreateApp(void)
  223. {
  224.     /* Set up the MUI application */
  225.     App = ApplicationObject,
  226.                 MUIA_Application_Author,      AUTHOR,
  227.                 MUIA_Application_Title,       "Yak_Prefs",
  228.                 MUIA_Application_Version,     versionstr,
  229.                 MUIA_Application_Copyright,   COPYRIGHT_STR,
  230.                 MUIA_Application_Description, "Yak Preferences",
  231.                 MUIA_Application_BrokerPri,   (LONG)broker_pri,
  232.                 MUIA_Application_SingleTask,  TRUE,
  233.                 MUIA_Application_Active,      TRUE,
  234.                 MUIA_Application_HelpFile,    PrefsHelp,
  235.                 MUIA_Application_Base,        "YAKPREFS",
  236.                 End;
  237.  
  238.     if (App)
  239.     {
  240.         DoMethod(App,
  241.                 MUIM_Notify, MUIA_Application_DoubleStart, TRUE,
  242.                 App, 
  243.                 2,
  244.                 MUIM_Application_ReturnID, ID_SHOW
  245.                 );
  246.  
  247.         DoMethod(App,
  248.                 MUIM_Notify,
  249.                 MUIA_Application_Iconified, TRUE,
  250.                 App,
  251.                 2,
  252.                 MUIM_Application_ReturnID, ID_HIDE
  253.                 );
  254.  
  255.         DoMethod(App,
  256.                 MUIM_Notify,
  257.                 MUIA_Application_Iconified, FALSE,
  258.                 App,
  259.                 2,
  260.                 MUIM_Application_ReturnID, ID_SHOW
  261.                 );
  262.  
  263.         get(App, MUIA_Application_BrokerPort, broker_mp);
  264.  
  265.         return TRUE;
  266.     }
  267.  
  268.     return FALSE;
  269. }
  270.  
  271. void
  272. DisposeApp(void)
  273. {
  274.     if (App) MUI_DisposeObject(App);
  275. }
  276.  
  277.  
  278. static LONG
  279. ProcessMsg(void)
  280. {
  281.     BOOL  running=TRUE;
  282.     ULONG signal, sigrcvd;
  283.     LONG  id;
  284.  
  285.     switch (id=DoMethod(App, MUIM_Application_Input, &signal))
  286.     {
  287.         case MUIV_Application_ReturnID_Quit:
  288.                 running = FALSE;
  289.                 break;
  290.  
  291.         case ID_HIDE:
  292.                 YakIconified = TRUE;
  293.                 break;
  294.  
  295.         case ID_SHOW:
  296.                 if (YakIconified)
  297.                 {
  298.                     YakIconified = FALSE;
  299.                     set(App, MUIA_Application_Iconified, FALSE);
  300.                 }
  301.                 if (NewPrefs)
  302.                 {
  303.                     /* Settings have changed */
  304.                     if (GetOrders(getString(Update_Restore_ORDERS),
  305.                                   getString(Prefs_modified_STRING)))
  306.                     {
  307.                         LoadSettings(ENV_CONFIG_FILE);
  308.                         HideInterface();
  309.                         curwinID = ROOT_WINDOW;
  310.                         NewPrefs = FALSE;
  311.                     }
  312.                 }
  313.                 if (!ShowYakInterface())
  314.                 {
  315.                     running = FALSE;
  316.                 }
  317.                 break;
  318.  
  319.         default:
  320.                 if (id>0)
  321.                     if ((*HandleReturnID)(id) != RET_OKAY)
  322.                         running = FALSE;
  323.                 break;
  324.     }
  325.  
  326.     if (running && signal) 
  327.     {
  328.         sigrcvd = Wait(signal | invariantsigflag);
  329.  
  330.         if (sigrcvd & notifysigflag)
  331.         {
  332.             NotifyNewPrefs();
  333.         }
  334.  
  335.         if (sigrcvd & SIGBREAKF_CTRL_C)
  336.             running = FALSE;
  337.     }
  338.     return running;
  339. }
  340.  
  341.  
  342. #endif          /* MUI */
  343. #ifdef BGUI
  344.  
  345. /*
  346.  *
  347.  *             BGUI Specific Code               NMC
  348.  *
  349.  */
  350.  
  351. struct Library  *BGUIBase;
  352. Object                  *MyCxObj;
  353.  
  354. /* close what we opened */
  355. static void
  356. CloseResources()
  357. {
  358.         /* NULL pointers are valid so don't waste time to test them */
  359.         CloseLibrary((struct Library *)IntuitionBase);
  360.         CloseLibrary((struct Library *)GfxBase);
  361.         CloseLibrary(CxBase);
  362.         CloseLibrary(DiskfontBase);
  363.         CloseLibrary(IconBase);
  364.         CloseLibrary(BGUIBase);
  365.         CloseLibrary(WorkbenchBase);
  366.         CloseLibrary(IFFParseBase);
  367.         CloseLibrary(KeymapBase);
  368.         CloseLocaleStuff();
  369. }
  370.  
  371. /* open libraries, devices that we need */
  372. static BOOL
  373. OpenResources(void)
  374. {
  375.         if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37L)) &&
  376.             (GfxBase       = (struct GfxBase *)OpenLibrary("graphics.library", 37L)) &&
  377.             (CxBase        = OpenLibrary("commodities.library", 37L)) &&
  378.             (DiskfontBase  = OpenLibrary("diskfont.library", 36L)) &&
  379.             (IconBase      = OpenLibrary("icon.library", 37L)) &&
  380.                         (BGUIBase      = OpenLibrary("bgui.library", 38L)) &&
  381.             (WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
  382.             (IFFParseBase  = OpenLibrary("iffparse.library", 37L)) &&
  383.             (KeymapBase    = OpenLibrary("keymap.library", 37L)))
  384.         {
  385.                 return TRUE;
  386.         }
  387.         CloseResources();
  388.         return FALSE;
  389. }
  390.  
  391. BOOL 
  392. CreateApp(void)
  393. {
  394. if (MyCxObj = CommodityObject,
  395.                 COMM_Name,              "Yak_Prefs",
  396.                 COMM_Title,             VERSION_BROKER,
  397.                 COMM_Description, "Yak Preferences",
  398.                 COMM_Priority,  broker_pri,
  399.                 COMM_Unique,    TRUE,
  400.                 COMM_Notify,    TRUE,
  401.                 COMM_ShowHide,  TRUE,
  402.                 EndObject)
  403.         {
  404.         EnableBroker(MyCxObj);
  405.         GetAttr(COMM_SigMask,MyCxObj,&cxsigflag);
  406.  
  407.         /* if FONT tooltype supplied, open font */
  408.         if (*FontName)
  409.                 {
  410.                 Attr.ta_Name = FontName;
  411.                 Attr.ta_YSize = FontSize;
  412.                 Attr.ta_Style = 0;
  413.                 Attr.ta_Flags = 0;
  414.                 TextFont = OpenDiskFont(&Attr);
  415.                 }
  416.         return(TRUE);
  417.         }
  418.  
  419. return(FALSE);
  420. }
  421.  
  422.  
  423. void
  424. DisposeApp(void)
  425. {
  426. DisposeObject(MyCxObj);
  427. if (TextFont) CloseFont(TextFont);
  428. }
  429.  
  430. /* monitor ports, act on messages */
  431. static LONG
  432. ProcessMsg(void)
  433. {
  434.     ULONG sigrcvd, msgid, msgtype,msgdata;
  435.     LONG returnvalue = 1L;
  436.  
  437.         if (curwin)
  438.                 {
  439.                 GetAttr(WINDOW_SigMask,curwinobj,&wndsigflag);
  440.                 GetAttr(WINDOW_AppMask,curwinobj,&appwinsigflag);
  441.                 }
  442.         else
  443.                 wndsigflag = appwinsigflag = 0;
  444.  
  445.     sigrcvd = Wait(invariantsigflag | wndsigflag | appwinsigflag);
  446.  
  447.     if (sigrcvd & appwinsigflag)
  448.     {
  449.         struct AppMessage *amsg;
  450.  
  451.                 while(amsg = GetAppMsg(curwinobj))
  452.         {
  453.             HandleAppMsg(NULL,NULL,&amsg);
  454.  
  455.             ReplyMsg((struct Message *)amsg);
  456.         }
  457.     }
  458.  
  459.     if (sigrcvd & wndsigflag)
  460.                 {
  461.                 while(curwin && ((msgid = HandleEvent(curwinobj)) != WMHI_NOMORE))
  462.                         {
  463.                         if ((*HandleReturnID)(msgid) != RET_OKAY)
  464.                     returnvalue = 0;
  465.                         }
  466.                 }
  467.  
  468.     if (sigrcvd & notifysigflag) /* settings have changed */
  469.     {
  470.         NotifyNewPrefs();
  471.     }
  472.  
  473.  
  474.         while(DoMethod(MyCxObj,CM_MSGINFO,
  475.                 &msgtype,&msgid,&msgdata) != CMMI_NOMORE)
  476.                 {
  477.         switch(msgtype)
  478.         {
  479.           case CXM_COMMAND:
  480.             switch(msgid)
  481.             {
  482.               case CXCMD_UNIQUE:
  483.               case CXCMD_APPEAR:
  484.                 YakIconified = FALSE;
  485.                 if (NewPrefs)
  486.                 {
  487.                     /* Settings have changed */
  488.                     if (GetOrders(getString(Update_Restore_ORDERS),
  489.                                   getString(Prefs_modified_STRING)))
  490.                     {
  491.                         LoadSettings(ENV_CONFIG_FILE);
  492.                         curwinID = ROOT_WINDOW;
  493.                         NewPrefs = FALSE;
  494.                     }
  495.                 }
  496.                 if (!ShowYakInterface())
  497.                 {
  498.                     returnvalue = 0L;
  499.                 }
  500.                 break;
  501.  
  502.               case CXCMD_DISAPPEAR:
  503.                 YakIconified = TRUE;
  504.                 HideInterface();
  505.                 break;
  506.  
  507.               case CXCMD_DISABLE:
  508.                 DisableBroker(MyCxObj);
  509.                 break;
  510.  
  511.               case CXCMD_ENABLE:
  512.                 EnableBroker(MyCxObj);
  513.                 break;
  514.  
  515.               case CXCMD_KILL:
  516.                 returnvalue = 0L;
  517.                 break;
  518.             }
  519.             break;
  520.         }
  521.     }
  522.  
  523.     if (sigrcvd & SIGBREAKF_CTRL_C)
  524.         returnvalue = 0L;
  525.  
  526.  
  527.     return(returnvalue);
  528. }
  529.  
  530.  
  531. #endif                  /* BGUI */
  532. #ifdef GADTOOLS
  533.  
  534. /*
  535.  *
  536.  *             GadTools Specific Code
  537.  *
  538.  */
  539.  
  540. #ifdef _DCC
  541. struct IntuitionBase  *IntuitionBase;
  542. struct GfxBase        *GfxBase;
  543.  
  544. struct Library  *IconBase, *GadToolsBase,
  545.                 *WorkbenchBase;
  546. #endif
  547.  
  548. struct Library    *AmigaGuideBase=NULL;
  549. AMIGAGUIDECONTEXT  AmigaGuideContext=NULL;
  550. ULONG              aguidesigflag=0L;
  551.  
  552. struct NewBroker newbroker = {
  553.     NB_VERSION,
  554.     "Yak_Prefs",                /* string to identify this broker */
  555.     VERSION_BROKER,
  556.     "Yak Preferences",
  557.     NBU_UNIQUE | NBU_NOTIFY,    /* Don't want any new commodities
  558.                                  * starting with this name.  If someone
  559.                                  * tries it, let me know */
  560.     COF_SHOW_HIDE
  561. };
  562.  
  563.  
  564. /* close what we opened */
  565. static void
  566. CloseResources()
  567. {
  568.         /* NULL pointers are valid so don't waste time to test them */
  569.         CloseLibrary((struct Library *)IntuitionBase);
  570.         CloseLibrary((struct Library *)GfxBase);
  571.         CloseLibrary(CxBase);
  572.         CloseLibrary(DiskfontBase);
  573.         CloseLibrary(IconBase);
  574.         CloseLibrary(GadToolsBase);
  575.         CloseLibrary(WorkbenchBase);
  576.         CloseLibrary(IFFParseBase);
  577.         CloseLibrary(KeymapBase);
  578.         CloseLocaleStuff();
  579. }
  580.  
  581.  
  582. /* open libraries, devices that we need */
  583. static BOOL
  584. OpenResources(void)
  585. {
  586.         if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37L)) &&
  587.             (GfxBase       = (struct GfxBase *)OpenLibrary("graphics.library", 37L)) &&
  588.             (CxBase        = OpenLibrary("commodities.library", 37L)) &&
  589.             (DiskfontBase  = OpenLibrary("diskfont.library", 36L)) &&
  590.             (IconBase      = OpenLibrary("icon.library", 37L)) &&
  591.             (GadToolsBase  = OpenLibrary("gadtools.library", 37L)) &&
  592.             (WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
  593.             (IFFParseBase  = OpenLibrary("iffparse.library", 37L)) &&
  594.             (KeymapBase    = OpenLibrary("keymap.library", 37L)))
  595.         {
  596.                 return TRUE;
  597.         }
  598.         CloseResources();
  599.         return FALSE;
  600. }
  601.  
  602.  
  603.  
  604. BOOL 
  605. CreateApp(void)
  606. {
  607.     if (broker_mp=CreateMsgPort())
  608.     {
  609.         newbroker.nb_Port = broker_mp;
  610.         cxsigflag = 1L << broker_mp->mp_SigBit;
  611.  
  612.         newbroker.nb_Pri = broker_pri;
  613.  
  614.         if (broker=CxBroker(&newbroker, NULL))
  615.         {
  616.             if ((AppMsgPort=CreateMsgPort()))
  617.             {
  618.                 appsigflag = 1L << AppMsgPort->mp_SigBit;
  619.                 return TRUE;
  620.             }
  621.             else PostError(getString(Allocation_ERR));
  622.         }
  623.     }
  624.     else PostError(getString(Allocation_ERR));
  625.  
  626.     return FALSE;
  627. }
  628.  
  629.  
  630. void
  631. DisposeApp(void)
  632. {
  633.     if (AppMsgPort) DeleteMsgPort(AppMsgPort);
  634.  
  635.     if (broker) DeleteCxObjAll(broker);
  636.             
  637.     if (broker_mp) DeleteMsgPort(broker_mp);
  638. }
  639.  
  640.  
  641.  
  642. void
  643. ShowYakHelp(char *aghelp, char *helpnode)
  644. {
  645.     struct NewAmigaGuide nag = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,0L,NULL,NULL,0L,NULL,NULL};
  646.  
  647.     if (!AmigaGuideBase)
  648.     {
  649.         if (AmigaGuideBase = OpenLibrary ("amigaguide.library", 34L))
  650.         {
  651.             nag.nag_Name      = aghelp;
  652.             nag.nag_PubScreen = PubScreenName;
  653.  
  654.             /* Open the help system */
  655.             AmigaGuideContext = OpenAmigaGuideAsync (&nag, NULL);
  656.  
  657.             /* Get our signal bits */
  658.             aguidesigflag = AmigaGuideSignal (AmigaGuideContext);
  659.  
  660.             /* Wait for a signal */
  661.             Wait (aguidesigflag);
  662.         }
  663.         else
  664.         {
  665.             PostError(getString(Resource_ERR));
  666.         }
  667.     }
  668.  
  669.     if (AmigaGuideBase)
  670.     {
  671.         char *cmd;
  672.  
  673.         if (cmd = AllocVec(strlen(helpnode)+8, MEMF_CLEAR))
  674.         {
  675.             /* Display the node */
  676.             sprintf(cmd, "LINK \"%s\"", helpnode);
  677.             SendAmigaGuideCmdA(AmigaGuideContext, cmd, NULL);
  678.  
  679.             FreeVec(cmd);
  680.         }
  681.     }
  682. }
  683.  
  684. void
  685. CloseYakHelp(void)
  686. {
  687.     /* Shutdown the help system */
  688.     if (AmigaGuideContext) CloseAmigaGuide (AmigaGuideContext);
  689.  
  690.     /* Close the library */
  691.     if (AmigaGuideBase) CloseLibrary (AmigaGuideBase);
  692.  
  693.     AmigaGuideContext = NULL;
  694.     AmigaGuideBase    = NULL;
  695.     aguidesigflag     = 0L;
  696. }
  697.  
  698.  
  699.  
  700. /* monitor cx port, act on messages */
  701. static LONG
  702. ProcessMsg(void)
  703. {
  704.     CxMsg *msg;
  705.     ULONG sigrcvd, msgid, msgtype;
  706.     LONG returnvalue = 1L;
  707.  
  708.     sigrcvd = Wait(invariantsigflag | wndsigflag | aguidesigflag);
  709.  
  710.     if (sigrcvd & aguidesigflag)
  711.     {
  712.         struct AmigaGuideMsg *agmsg;
  713.         BOOL   err=FALSE;
  714.  
  715.         /* process amigaguide messages */
  716.         while (agmsg = GetAmigaGuideMsg (AmigaGuideContext))
  717.         {
  718.             /* check message types */
  719.             switch (agmsg->agm_Type)
  720.             {
  721.                 case ToolCmdReplyID:
  722.                 case ToolStatusID:
  723.                 case ShutdownMsgID:
  724.                     if (agmsg->agm_Pri_Ret)
  725.                     {
  726.                         PostError("\"%s\"\n%s", PrefsHelp, GetAmigaGuideString(agmsg->agm_Sec_Ret));
  727.  
  728.                         err = TRUE;
  729.                     }
  730.                     break;
  731.  
  732.                 default:
  733.                     break;
  734.             }
  735.  
  736.             /* Reply to the message */
  737.             ReplyAmigaGuideMsg (agmsg);
  738.         }
  739.  
  740.         if (err)
  741.             CloseYakHelp();
  742.     }
  743.  
  744.     if (sigrcvd & appsigflag)
  745.     {
  746.         struct AppMessage *amsg;
  747.  
  748.         while (amsg=(struct AppMessage *)GetMsg(AppMsgPort))
  749.         {
  750.             HandleAppMsg(NULL,NULL,&amsg);
  751.  
  752.             ReplyMsg((struct Message *)amsg);
  753.         }
  754.     }
  755.  
  756.     if (sigrcvd & wndsigflag)
  757.         if ((*HandleIDCMP)() != RET_OKAY)
  758.             returnvalue = 0;
  759.  
  760.     if (sigrcvd & notifysigflag) /* settings have changed */
  761.     {
  762.         NotifyNewPrefs();
  763.     }
  764.  
  765.  
  766.     while(msg = (CxMsg *)GetMsg(broker_mp))
  767.     {
  768.         msgid   = CxMsgID(msg);
  769.         msgtype = CxMsgType(msg);
  770.         ReplyMsg((struct Message *)msg);
  771.  
  772.         switch(msgtype)
  773.         {
  774.           case CXM_COMMAND:
  775.             switch(msgid)
  776.             {
  777.               case CXCMD_UNIQUE:
  778.               case CXCMD_APPEAR:
  779.                 YakIconified = FALSE;
  780.                 if (NewPrefs)
  781.                 {
  782.                     /* Settings have changed */
  783.                     if (GetOrders(getString(Update_Restore_ORDERS),
  784.                                   getString(Prefs_modified_STRING)))
  785.                     {
  786.                         LoadSettings(ENV_CONFIG_FILE);
  787.                         curwinID = ROOT_WINDOW;
  788.                         NewPrefs = FALSE;
  789.                     }
  790.                 }
  791.                 if (!ShowYakInterface())
  792.                 {
  793.                     returnvalue = 0L;
  794.                 }
  795.                 break;
  796.  
  797.               case CXCMD_DISAPPEAR:
  798.                 YakIconified = TRUE;
  799.                 CloseYakHelp();
  800.                 HideInterface();
  801.                 break;
  802.  
  803.               case CXCMD_DISABLE:
  804.                 ActivateCxObj(broker, 0L);
  805.                 break;
  806.  
  807.               case CXCMD_ENABLE:
  808.                 ActivateCxObj(broker, 1L);
  809.                 break;
  810.  
  811.               case CXCMD_KILL:
  812.                 returnvalue = 0L;
  813.                 break;
  814.             }
  815.             break;
  816.         }
  817.     }
  818.  
  819.     if (sigrcvd & SIGBREAKF_CTRL_C)
  820.         returnvalue = 0L;
  821.  
  822.  
  823.     return returnvalue;
  824. }
  825.  
  826. #endif  /* GADTOOLS */
  827.  
  828. /*
  829.  *
  830.  *             Shared Code
  831.  *
  832.  */
  833.  
  834. static SAVEDS LONG ASM
  835. HandleAppMsg(REG(a0) struct Hook *hook,
  836.              REG(a2) APTR obj,
  837.              REG(a1) struct AppMessage **am)
  838. {
  839.     struct AppMessage *amsg=*am;
  840.  
  841.     if (PrefsFile) FreeVec(PrefsFile);
  842.  
  843.     if (PrefsFile=AllocVec(256, MEMF_CLEAR))
  844.     {
  845.         /* Get first argument */
  846.         struct WBArg *wbarg=amsg->am_ArgList;
  847.  
  848.         if (NameFromLock(wbarg->wa_Lock, PrefsFile, 256))
  849.            AddPart(PrefsFile, wbarg->wa_Name, 256);
  850.     }
  851.  
  852. #ifdef MUI
  853.     if (YakIconified)
  854.     {
  855.         YakIconified = FALSE;
  856.         DoMethod(App, MUIM_Application_PushMethod, App, 3, MUIM_Set, MUIA_Application_Iconified, FALSE);
  857.     }
  858. #endif
  859.  
  860.     CloseWindowID(curwinID);
  861.  
  862.     LoadSettings(PrefsFile);
  863.  
  864.     curwinID = ROOT_WINDOW;
  865.     ShowYakInterface();
  866.  
  867.     return 0L;
  868. }
  869.  
  870.  
  871. static void
  872. NotifyNewPrefs(void)
  873. {   
  874.     if (!YakIconified)
  875.     {
  876.         /* GUI is opened */
  877.         DisableYakInterface();
  878.         if (GetOrders(getString(Update_Restore_ORDERS),
  879.                       getString(Prefs_modified_STRING)))
  880.         {
  881.             CloseWindowID(curwinID);
  882.             
  883.             LoadSettings(ENV_CONFIG_FILE);
  884.             
  885.             curwinID = ROOT_WINDOW;
  886.             NewPrefs = FALSE;
  887.             ShowYakInterface();
  888.         }
  889.         else
  890.             EnableYakInterface();
  891.     }
  892.     else
  893.         NewPrefs = TRUE;   /* Yak-Prefs: HIDE mode */
  894. }
  895.  
  896.  
  897. void
  898. EndNotification(void)
  899. {
  900.         EndNotify(&NotifyRequest);
  901.         FreeSignal(notifysigbit);
  902. }
  903.  
  904. void
  905. SetNotification(void)
  906. {
  907.         /* Allocate a signalsbit */
  908.         if ((notifysigbit = AllocSignal(-1L)) != -1)
  909.         {
  910.                 notifysigflag = 1 << notifysigbit;
  911.  
  912.                 /* Initialize notification request */
  913.  
  914.                 NotifyRequest.nr_Name = ENV_CONFIG_FILE;
  915.                 NotifyRequest.nr_Flags = NRF_SEND_SIGNAL; 
  916.                 /* Signal this task */
  917.                 NotifyRequest.nr_stuff.nr_Signal.nr_Task = (struct Task *) FindTask(NULL);
  918.                 /* with this signals bit */
  919.                 NotifyRequest.nr_stuff.nr_Signal.nr_SignalNum = notifysigbit;
  920.  
  921.                 StartNotify(&NotifyRequest);
  922.         }
  923. }
  924.  
  925.  
  926. static void
  927. FreePatterns()
  928. {
  929.         UWORD i;
  930.  
  931.         for (i = 0; i < NUM_PATTERNS; i++)
  932.                 if (patterns[i].pat) FreeVec(patterns[i].pat);
  933. }
  934.  
  935.  
  936. #ifdef _DCC
  937. void _waitwbmsg(void);
  938. static void
  939. uncalled(void)
  940. {
  941.     _waitwbmsg();
  942. }
  943. #endif
  944.  
  945. void 
  946. #ifdef FORTIFY
  947. main(int argc, char **argv)
  948. #else
  949. STACKARGS MAIN(char *arg)             /* Yak: multi-function commodity */
  950. #endif
  951. {
  952. #ifdef FORTIFY
  953.     Fortify_EnterScope();
  954.     Fortify_CheckAllMemory();
  955. #endif
  956.  
  957.     if (OpenResources())
  958.     {
  959.         if (WBMsg)              /* WB Startup */
  960.         {
  961.             struct WBArg *wbarg=&WBMsg->sm_ArgList[WBMsg->sm_NumArgs-1];
  962.  
  963.             if (WBMsg->sm_NumArgs > 1)
  964.             {
  965.                 BPTR dirlock;
  966.  
  967.                 /* Go to icon directory */
  968.                 dirlock = CurrentDir(wbarg->wa_Lock);
  969.  
  970.                 /* Install prefs */
  971.                 CopyFile(wbarg->wa_Name, ENV_CONFIG_FILE);
  972.  
  973.                 /* Go back and exit... */
  974.                 CurrentDir(dirlock);
  975.                 FreeOurIcon();
  976.                 CloseResources();
  977.                 return;
  978.             }
  979.             else
  980.             {
  981.                 /* Get Program Name */
  982.                 if (ProgramName=AllocVec(256, MEMF_CLEAR))
  983.                 {
  984.                     if (NameFromLock(wbarg->wa_Lock, ProgramName, 256))
  985.                         AddPart(ProgramName, wbarg->wa_Name, 256);
  986.                 }
  987.             }
  988.         }
  989.         else                    /* CLI Startup */
  990.         {
  991.             /* Get Program Name */
  992.             if (ProgramName=AllocVec(256, MEMF_CLEAR))
  993.             {
  994.                 BPTR lock;
  995.                 GetProgramName(ProgramName, 256);
  996.                 if (lock=Lock(ProgramName, ACCESS_READ))
  997.                 {
  998.                     NameFromLock(lock, ProgramName, 256);
  999.                     UnLock(lock);
  1000.                 }
  1001.             }
  1002.         }
  1003.  
  1004.         /* */
  1005.         if (PrefsFile=AllocVec(256, MEMF_CLEAR))
  1006.         {
  1007.             BPTR lock=GetProgramDir();
  1008.             if (NameFromLock(lock, PrefsFile, 256))
  1009.                 AddPart(PrefsFile, "Presets/", 256);
  1010.         }
  1011.  
  1012.         /* process tool-types */
  1013.         GetOurIcon(ProgramName);
  1014.  
  1015.         /* Open the right locale if tooltype LANGUAGE is used */
  1016.         OpenLocaleStuff(TTString("LANGUAGE",NULL));
  1017.  
  1018.         /* Amigaguide */
  1019.         PrefsHelp = DupStr(TTString("PREFSHELP", "Yak.guide"));
  1020.  
  1021. #if defined(GADTOOLS) || defined(BGUI)
  1022.         ReadFont( FontName, &FontSize);
  1023. #endif
  1024.  
  1025.         CreateIcons  = TTBool("CREATEICONS", TRUE);
  1026.  
  1027.         broker_pri = (BYTE)TTInt("CX_PRIORITY", 0);
  1028.  
  1029.         FreeOurIcon();
  1030.  
  1031.         if (CreateApp())
  1032.         {
  1033.             InitYakHotKeyList();
  1034.  
  1035.             LoadSettings(ENV_CONFIG_FILE);
  1036.  
  1037. #ifdef GADTOOLS
  1038.             ActivateCxObj(broker, 1L);
  1039. #endif
  1040.             /*
  1041.              * We want to be notified each time either
  1042.              * ENV:yak.prefs is modified (by Yak Preferences)
  1043.              */
  1044.             SetNotification();
  1045.  
  1046.             /* these are the signals waited for, + window sig */
  1047.             invariantsigflag = SIGBREAKF_CTRL_C | cxsigflag | appsigflag | notifysigflag;
  1048.  
  1049.             if (ShowYakInterface())
  1050.             {
  1051.                 while (ProcessMsg())
  1052.                     ;
  1053.  
  1054. #ifdef GADTOOLS
  1055.                 CloseYakHelp();
  1056. #endif
  1057.                 HideInterface();
  1058.             }
  1059.  
  1060.             DeleteYakHotKeyList();
  1061.  
  1062.             EndNotification();
  1063.  
  1064.             FreePatterns();
  1065.  
  1066.             CleanMouseCycling();
  1067.         }
  1068.  
  1069.         DisposeApp();
  1070.  
  1071.         if (PrefsHelp)   FreeVec(PrefsHelp);
  1072.         if (ProgramName) FreeVec(ProgramName);
  1073.         if (PrefsFile)   FreeVec(PrefsFile);
  1074.  
  1075. #ifdef FORTIFY
  1076.                 Fortify_LeaveScope();
  1077. #endif
  1078.  
  1079.         CloseResources();
  1080.     }
  1081.     else PostError(getString(Resource_ERR));
  1082.  
  1083. }
  1084.  
  1085.